<?php
class TourAction extends ZMAction {
    public function index(){
        $start = $_REQUEST['start'];
        $limit = $_REQUEST["limit"];
        $sortArr = json_decode(str_replace("\\\"","\"",$_REQUEST['sort']),true);
        if($sortArr){
            $sort = "";
            foreach($sortArr as $item){
                if($sort != '') $sort .= ',';
                $sort .= $item['property']. ' ' . $item['direction'];
            }
        }
        if(!is_numeric($start)){
            $start = 0;
        }
        if(!is_numeric($limit)){
            $limit = 20;
        }
        $Model = M("Tour");
        $count = $Model->count();
        if(empty($sort)){
            $result = $Model->limit("$start,$limit")->select();
        }else{
            $result = $Model->order("$sort $dir")->limit("$start,$limit")->select();
        }
        if($result == null){
            $result = array();
        }
        $this->ajaxRetArray($result,$count);    
    }
    public function save(){
        $model = M('Tour');
        $tid = $_REQUEST['tid'];
        
        $data = $model->create();
        
        if(empty($tid)){
            $tid = $model->add($data);
            if($ddid===false){
                $this->error('保存错误!');
                return;
            }
        }else{
            $model->save($data);
        }
        
        $s = str_replace("\\\"","\"",$_REQUEST['pos']);
        $s = str_replace("\\\\","\\",$s);
        $posArr = json_decode($s,true);
        
        $modelPos = M('TourPos');
        $modelPos->where("tid=$tid")->delete();
        foreach($posArr as $row){
            unset($data);
            $data['tid'] = $tid;
            $data['position'] = $row['position'];
            $data['orientation'] = $row['orientation'];
            $modelPos->add($data);
        }
        
        $s = str_replace("\\\"","\"",$_REQUEST['items']);
        $s = str_replace("\\\\","\\",$s);
        $itemArr = json_decode($s,true);

        $modelItems = M('TourItems');
        $modelItems->where("tid=$tid")->delete();
        
        foreach($itemArr as $row){
            unset($data);
            $data['tid'] = $tid;
            $data['dname'] = $row['dname'];
            $data['did'] = $row['did'];
            $modelItems->add($data);
        }
        
        $this->success('ok');
    }
    public function del(){
        $tid = $_REQUEST['tid'];
        $model = M('Tour');
        $model->where("tid=$tid")->delete();
        
        $modelPos = M('TourPos');
        $modelPos->where("tid=$tid")->delete();

        $modelItems = M('TourItems');
        $modelItems->where("tid=$tid")->delete();
        
        $this->success('ok');
    }
    
    public function getPos(){
        $tid = $_REQUEST['tid'];
        
        $modelPos = M('TourPos');
        $res = $modelPos->where("tid=$tid")->order('pid')->select();
        if($res){
            
        }else{
            $res = array();
        }
        $this->ajaxRetArray($res);
    }
    
    public function getItems(){
        $tid = $_REQUEST['tid'];
        
        $modelItems = M('TourItems');
        $modelYc = M('Yc');
        $res = $modelItems->where("tid=$tid")->order('iid')->select();
        if($res){
            foreach($res as &$row){
                $did = $row['did'];
                $resYc = $modelYc->where("id=$did")->find();
                if($resYc){
                    $row['desc'] = '[' . $resYc['sname'] . ']' .$resYc['dname'];
                }
            }
        }else{
            $res = array();
        }
        $this->ajaxRetArray($res);
    }
    
    public function getEnum(){
        $model = M('Tour');
        $res = $model->field('tid,tourname')->select();
        foreach($res as &$row){
            $row['id'] = $row['tid'];
            $row['text'] = $row['tourname'];
        }
        $this->ajaxRetArray($res);
    }
    
    public function getCombo(){
        $model = M('Tour');
        $res = $model->field('tid,tourname')->select();
        $ret = array();
        $data['id'] = '0';
        $data['text'] = '所有';
        $ret[] = $data;
        foreach($res as $row){
            $data['id'] = $row['tid'];
            $data['text'] = $row['tourname'];
            $ret[] = $data;
        }
        $this->ajaxRetArray($ret);
    }
    
    public function startTour(){
        $tid = $_REQUEST['tid'];
        $modelTour = M('Tour');
        $resTour = $modelTour->where("tid=$tid")->find();
        if($resTour){
            $modelPos = M('TourPos');
            $res = $modelPos->where("tid=$tid")->select();
            $count = count($res);
            $per = 1.0 / ($count-1);
            $pos = '';
            $ori = '';
            $key = '0';
            for($i=1;$i<$count-1;$i++){
                $key .= ' ' . ($per*$i);
            }
            $key .= ' 1';
            foreach($res as $row){
                $pos .= ' ' . $row['position'];
                $ori .= ' ' . $row['orientation'];
            }
            
            $ret['key'] = $key;
            $ret['pos'] = $pos;
            $ret['ori'] = $ori;
            $ret['timelen'] = $count*10;
            
            //保存巡视记录
            $modLog = M('TourLog');
            $data['logname'] = $resTour['tourname'];
            $data['tid'] = $tid;
            $data['userid'] = $_SESSION['user']['userid'];
            $data['username'] = $_SESSION['user']['username'];
            $logid = $modLog->add($data);
            if($logid !== false){
                $modTourItem = M('TourItems');
                $modTourLogData = M('TourLogData');
                $modYc = M('Yc');
                $resItems = $modTourItem->where("tid=$tid")->select();
                foreach($resItems as $row){
                    unset($data);
                    $data['logid'] = $logid;
                    $data['dname'] = $row['dname'];
                    $data['did'] = $row['did'];
                    
                    $resYc = $modYc->where("id=".$row['did'])->find();
                    if($resYc){
                        $data['val'] = $resYc['val'];
                    }else{
                        $data['val'] = 0;
                    }
                    $modTourLogData->add($data);
                }
            }
            
            $this->ajaxReturn($ret,'',true);
        }else{
            $this->error('巡视线路不存在!');
        }
    }
    
    public function loglist(){
        $start = $_REQUEST['start'];
        $limit = $_REQUEST["limit"];
        $sortArr = json_decode(str_replace("\\\"","\"",$_REQUEST['sort']),true);
        if($sortArr){
            $sort = "";
            foreach($sortArr as $item){
                if($sort != '') $sort .= ',';
                $sort .= $item['property']. ' ' . $item['direction'];
            }
        }
        if(!is_numeric($start)){
            $start = 0;
        }
        if(!is_numeric($limit)){
            $limit = 20;
        }
        $startdate = $_REQUEST['startdate'];
        if(empty($startdate)){
            $startdate = date('Y-').'01-01';
        }
        $enddate = $_REQUEST['enddate'];
        if(empty($enddate)){
            $enddate = date('Y-m-d');
        }
        $tid = $_REQUEST['tid'];
        if(!empty($tid)){
            $where['tid'] = $tid;
        }
        $enddate = date('Y-m-d',strtotime('+1 day',strtotime($enddate)));
        $where['_string'] = "pdate>='$startdate' and pdate<'$enddate'";
        
        $Model = M("TourLog");
        $count = $Model->where($where)->count();
        if(empty($sort)){
            $result = $Model->where($where)->limit("$start,$limit")->select();
        }else{
            $result = $Model->where($where)->order("$sort $dir")->limit("$start,$limit")->select();
        }
        if($result){
        }else{
            $result = array();
        }
        $this->ajaxRetArray($result,$count);    
    }
    
    public function logitem(){
        $logid = $_REQUEST['logid'];
        $model = M('TourLogData');
        $where['logid'] = $logid;
        $res = $model->where($where)->select();
        $this->ajaxRetArray($res);
    }
}